<?php
declare (strict_types = 1);

namespace app\middleware;
use app\BaseController;
use think\facade\Session;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use app\model\Admin as AD;
use app\model\Role as RO;
use app\model\Admin_Role as AR;
use app\model\Node as ND;



class UrlCheck
{
    use \liliuwei\think\Jump;
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {

        $admin = Session::get('admin');
        $admin_id = $admin['id'];
        if($admin['admin_name']!="admin"){

        $admin_url = Db::table('admin')
            ->alias('a')
            ->where('a.id',$admin_id)
            ->leftJoin('admin_role ar',"ar.admin_id=a.id")
            ->leftJoin('role_node rn',"ar.role_id=rn.role_id")
            ->leftJoin('node n',"n.id=rn.node_id")
            ->field("n.node_url")
            ->select()
            ->toArray();

        $url_arr = $request->baseUrl();
        $url_arr = $var=explode("/",$url_arr);

        $url = "/".$url_arr['1']."/".$url_arr['2'];

        $urls = [];
        foreach ($admin_url as $k=>$v){
            $urls[] = $v['node_url'];
        }
//            $urls[] = "/".$url_arr['1']."/data";
//            $urls[] = "/".$url_arr['1']."/LoginOut";
//            $urls[]  = "/".$url_arr['1']."/record";



            $de_url[] = "/".$url_arr['1']."/data";  //首页获取数据的方法
            $de_url[] = "/".$url_arr['1']."/LoginOut";  //退出中间件的方法
            $de_url[] = "/".$url_arr['1']."/record";  //无限极分类的方法



       if(!in_array($url,$urls)){
           if(!in_array($url,$de_url)) {
               $this->error("没有权限", "/index/welcome");
           }
         }
            return $next($request);
        }

        return $next($request);

    }
}
